//
//  DestinationViewController.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "DestinationViewController.h"

#import "ZHFCollectionViewCell.h"

#import "ZHFCollectionReusableView.h"

#import "MBProgressHUD.h"

#import "Resources.h"

#import "ZHFDestination.h"

#import "ZHFData.h"

#import "ZHFAreaViewController.h"

#import "ZHFAllViewController.h"

#import "GSYTabbarViewController.h"

@interface DestinationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,ZHFDestinationReusableViewDelegate>

@property (nonatomic,strong) UICollectionView *mainCollection;

@property (nonatomic,strong) NSMutableArray *array;

@property (nonatomic,strong) MBProgressHUD *mbBarPrpgressV;

@end

@implementation DestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"目的地";
    
    self.array = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 32 * ADAPT_WIDTH) / 2, (SCREEN_WIDTH - 32 * ADAPT_WIDTH) / 2);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10 * ADAPT_WIDTH, 10, 10 * ADAPT_WIDTH);
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 70);
    
    self.mainCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 60) collectionViewLayout:flowLayout];
    self.mainCollection.backgroundColor = self.view.backgroundColor;
    self.mainCollection.delegate = self;
    self.mainCollection.dataSource = self;
    
    [self.mainCollection registerClass:[ZHFCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [self.view addSubview:self.mainCollection];
    
    [self.mainCollection registerClass:[ZHFCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"destination"];
    
    [self.view bringSubviewToFront:self.getDataBtn];
    self.mbBarPrpgressV = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [self AFNWorking];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nightAction:) name:@"night" object:nil];
    
}

- (void)nightAction:(NSNotification *)notification {
    
    self.mainCollection.backgroundColor = [notification object];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"1"]) {
        
    }
    else {
        
    }
    self.view.backgroundColor = [notification object];
    [self.mainCollection reloadData];
}

#pragma mark -- collectionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
//    NSLog(@"___ %ld",_array.count);
    return _array.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    ZHFDestination *destination = [_array objectAtIndex:section];
    
    if (destination.array.count >= 4) {
        return 4;
    }
    return destination.array.count;
}

#pragma mark 头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ZHFCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"destination" forIndexPath:indexPath];
    
    //第五步:指定代理人
    reusableView.delegate = self;
    
//    reusableView.backgroundColor = self.view.backgroundColor;
//    reusableView.backgroundColor = [UIColor orangeColor];
    if ([[[self.array objectAtIndex:indexPath.section] array] count] > 4) {
        [reusableView createAllView:NO];
    }
    else {
        [reusableView createAllView:YES];
    }
    reusableView.num = indexPath.section;
    ZHFDestination *destination = [self.array objectAtIndex:indexPath.section];
    reusableView.titleText = destination.title;
    
    return reusableView;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZHFCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = self.view.backgroundColor;
    cell.data = [[[self.array objectAtIndex:indexPath.section] array] objectAtIndex:indexPath.row];
    
    return cell;
}

//cell点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZHFAreaViewController *areaVC = [[ZHFAreaViewController alloc] init];
    
    ZHFData *data = [[[self.array objectAtIndex:indexPath.section] array] objectAtIndex:indexPath.row];
    areaVC.Id = data.Id;
    areaVC.navigationItem.title = [NSString stringWithFormat:@"%@攻略",data.name_zh_cn];
    
    GSYTabbarViewController *tabBarVC = (GSYTabbarViewController *)self.tabBarController;
    [tabBarVC isHiddenTabBar:YES withViewController:areaVC];
    
    [self.navigationController pushViewController:areaVC animated:YES];
}

#pragma mark 跳转全部目的地
//协议第六步:实现协议方法
- (void)pushAllViewController:(NSInteger)num {
    
    ZHFAllViewController *allVC = [[ZHFAllViewController alloc] init];
    allVC.array = self.array;
    allVC.index = num;
    GSYTabbarViewController *tabbar = (GSYTabbarViewController *)self.tabBarController;
    [tabbar isHiddenTabBar:YES withViewController:allVC];
    [self.navigationController pushViewController:allVC animated:YES];
}


#pragma mark - AFN网络请求
- (void)AFNWorking {
    
    self.getDataBtn.hidden = YES;
    [self.mbBarPrpgressV show:YES];
    NSString *strUrl = @"http://chanyouji.com/api/destinations.json?page=1";
    
    [Resources getDataWithName:strUrl block:^(id result) {
        
        NSArray *bigArray = result;
        for (NSDictionary *dic in bigArray) {
            ZHFDestination *destination = [[ZHFDestination alloc] init];
            [destination setValuesForKeysWithDictionary:dic];
            [self.array addObject:destination];
        }
        
        [self.mbBarPrpgressV hide:YES];
        [self.mainCollection reloadData];
        
    } error:^{
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if (self.array.count == 0) {
                self.getDataBtn.hidden = NO;
            }
            [self.mbBarPrpgressV hide:YES];
        }];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
